package 算法.leetcode.offer;

import 算法.leetcode.offer.base.TreeNode;

/**
 * 请完成一个函数，输入一个二叉树，该函数输出它的镜像。
 *
 * 例如输入：
 *
 *      4
 *    /   \
 *   2     7
 *  / \   / \
 * 1   3 6   9
 * 镜像输出：
 *
 *      4
 *    /   \
 *   7     2
 *  / \   / \
 * 9   6 3   1
 *
 *  
 *
 * 示例 1：
 *
 * 输入：root = [4,2,7,1,3,6,9]
 * 输出：[4,7,2,9,6,3,1]
 *
 * @author lchenglong
 * @date 2022/3/18
 */
public class Offer27 {

    public static void main(String[] args) {
        TreeNode t4 = new TreeNode(4),t2 = new TreeNode(2),t7 = new TreeNode(7);
        TreeNode t1 = new TreeNode(1),t3 = new TreeNode(3),t6 = new TreeNode(6);
        TreeNode t9 = new TreeNode(9);
        t4.left = t2;
        t4.right = t7;
        t2.left = t1;
        t2.right = t3;
        t7.left = t6;
        t7.right = t9;
        Offer27 instance = new Offer27();
        TreeNode.print(t4);
        TreeNode ans = instance.mirrorTree(t4);
        TreeNode.print(ans);
    }
    public TreeNode mirrorTree(TreeNode root) {
        if (root!=null) {
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
            mirrorTree(root.left);
            mirrorTree(root.right);
        }
        return root;
    }


}
